/*
 * Copyright (c) 2001-2023 cnstrong.cn Corporation Limited. All rights reserved.
 * This software is the confidential and proprietary information of cnstrong.cn Company.
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with cnstrong.cn.
 */
package com.tree.config.common;

import com.alibaba.fastjson.JSONObject;
import com.tree.config.exception.WeTreeException;
import com.tree.config.repository.dataobject.ProjectConfigDefineDO;
import com.tree.config.repository.dataobject.ProjectConfigValueDO;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;

/**
 * 配置转换工具类
 *
 * @author Braveheart
 * @version V1.0
 * @since 2023-12-29
 */
public class ConfigCovertTool {
    private ConfigCovertTool() {
    }

    /**
     * 配置转成json字符串
     */
    public static String covertToJson(List<ProjectConfigDefineDO> configDefineDOList,
        List<ProjectConfigValueDO> configValueDOList) {
        JSONObject result = new JSONObject();
        if (CollectionUtils.isEmpty(configDefineDOList)) {
            return null;
        }
        if (CollectionUtils.isEmpty(configValueDOList)) {
            return result.toJSONString();
        }
        // cfg_code3 -> List 映射Map
        Map<String, List<ProjectConfigValueDO>> cfgCode3Map = configValueDOList.stream()
            .collect(Collectors.groupingBy(p -> p.getCfgCode3()));

        // 构建json
        for (ProjectConfigDefineDO configDefineDO : configDefineDOList) {
            if (Objects.equals(configDefineDO.getCfgCode3(), "-1")) {
                continue;
            }
            if (isBaseType(configDefineDO.getCfgParameterType())) {
                List<ProjectConfigValueDO> valueDOList = cfgCode3Map.get(configDefineDO.getCfgCode3());
                if (CollectionUtils.isNotEmpty(valueDOList)) {
                    result.put(configDefineDO.getCfgCode3(), valueDOList.get(0).getCfgValue1());
                }
            } else if (isListType(configDefineDO.getCfgParameterType())) {
                List<ProjectConfigValueDO> valueDOList = cfgCode3Map.get(configDefineDO.getCfgCode3());
                if (CollectionUtils.isNotEmpty(valueDOList)) {
                    List<String> valueList = valueDOList.stream()
                        .map(p -> p.getCfgValue1()).collect(Collectors.toList());
                    result.put(configDefineDO.getCfgCode3(), valueList);
                }
            } else if (isMapType(configDefineDO.getCfgParameterType())) {
                List<ProjectConfigValueDO> valueDOList = cfgCode3Map.get(configDefineDO.getCfgCode3());
                if (CollectionUtils.isNotEmpty(valueDOList)) {
                    Map<String, String> valueMap = valueDOList.stream()
                        .collect(Collectors.toMap(p -> p.getCfgValue1(), p -> p.getCfgValue2(), (o1, o2) -> o1));
                    result.put(configDefineDO.getCfgCode3(), valueMap);
                }
            } else {
                throw new WeTreeException("配置参数类型不合法[" + configDefineDO.getCfgParameterType() + "]");
            }
        }
        return result.toJSONString();
    }

    /**
     * 是否是基础类型
     *
     * @param cfgParameterType
     * @return
     */
    private static boolean isBaseType(String cfgParameterType) {
        return cfgParameterType.startsWith("java.lang");
    }

    /**
     * 是否是List类型
     *
     * @param cfgParameterType
     * @return
     */
    private static boolean isListType(String cfgParameterType) {
        return cfgParameterType.startsWith("java.util.List");
    }

    /**
     * 是否是Map类型
     *
     * @param cfgParameterType
     * @return
     */
    private static boolean isMapType(String cfgParameterType) {
        return cfgParameterType.startsWith("java.util.Map");
    }
}
